<template>
    <div class="div">
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户名" prop="username">
                <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="phone">
                <el-input type="text" v-model="ruleForm.phone" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
                <el-input type="text" v-model="ruleForm.email" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="公司" prop="company">
                <el-select v-model="ruleForm.company" clearable placeholder="请选择">
                    <el-option v-for="item in company_list" :key="item.id" :label="item.name" :value="item.id"> </el-option>
                </el-select>
            </el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        </el-form>
    </div>
</template>

<script>
import VueCropper from 'vue-cropperjs';
import { permission, Register } from '../../api/api.js';
export default {
    name: 'upload',
    data: function() {
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入用户名'));
            } else {
                if (this.ruleForm.password !== '') {
                    this.$refs.ruleForm.validateField('checkPass');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                callback();
            }
        };
        var validatePass3 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入手机号'));
            } else {
                if (!/^1[3456789]\d{9}$/.test(value)) {
                    callback(new Error('手机号码有误，请重填'));
                } else {
                    this.is_phone = true;
                    callback();
                }
            }
        };
        var validatePass5 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入邮箱'));
            } else {
                callback();
            }
        };
        var validatePass6 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请选择公司'));
            } else {
                callback();
            }
        };
        return {
            ruleForm: {
                username: '',
                password: '',
                phone: '',
                email: '',
                company: ''
            },
            rules: {
                username: [{ validator: validatePass, trigger: 'blur' }],
                password: [{ validator: validatePass2, trigger: 'blur' }],
                phone: [{ validator: validatePass3, trigger: 'blur' }],
                email: [{ validator: validatePass5, trigger: 'blur' }],
                company: [{ validator: validatePass6, trigger: 'blur' }]
            },
            company_list: []
        };
    },
    components: {
        VueCropper
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    Register({
                        username: this.ruleForm.username,
                        password: this.ruleForm.password,
                        phone: this.ruleForm.phone,
                        code: '1433223',
                        email: this.ruleForm.email,
                        company: this.ruleForm.company
                    }).then(res => {
                        this.$message(res.data.msg);
                    });
                } else {
                    return false;
                }
            });
        },
        // 获取所有公司
        get_company() {
            permission().then(res => {
                this.company_list = res.data.company;
            });
        }
    },
    created() {
        // this.cropImg = this.defaultSrc;
    },
    mounted: function() {
        this.get_company();
    }
};
</script>

<style scoped>
.content-title {
    font-weight: 400;
    line-height: 50px;
    margin: 10px 0;
    font-size: 22px;
    color: #1f2f3d;
}
.pre-img {
    width: 100px;
    height: 100px;
    background: #f8f8f8;
    border: 1px solid #eee;
    border-radius: 5px;
}
.crop-demo {
    display: flex;
    align-items: flex-end;
}
.crop-demo-btn {
    position: relative;
    width: 100px;
    height: 40px;
    line-height: 40px;
    padding: 0 20px;
    margin-left: 30px;
    background-color: #409eff;
    color: #fff;
    font-size: 14px;
    border-radius: 4px;
    box-sizing: border-box;
}
.crop-input {
    position: absolute;
    width: 100px;
    height: 40px;
    left: 0;
    top: 0;
    opacity: 0;
    cursor: pointer;
}
.div {
    margin: auto;
    width: 400px;
}
</style>